/*
 * 文件名：BillPayResponce.java	 
 * 时     间：下午1:44:57
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.message.handler.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;

import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.CommonContains;
import com.local.model.CarParkIn;
import com.local.model.LocalCarInRepertory;
import com.local.model.LocalCouponCash;
import com.local.model.LocalPayBillCopy;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.LocalCarInRepertoryService;
import com.local.service.face.LocalCouponCashService;
import com.local.service.face.ParkwellChargeBytimeService;
import com.local.service.face.ParkwellPayBillService;
import com.message.entity.BillInfo;
import com.message.entity.CouponInfo;
import com.message.entity.TransDataVo;
import com.message.handler.face.ResponceHandler;
import com.qianhai.common.constants.CommonContans;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: BillPayResponce
 * @author Administrator
 * @date 2015年10月11日 下午1:44:57
 */
public class BillPayResponce implements ResponceHandler
{
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public TransDataVo resoponse(ApplicationContext srpingApp,
			TransDataVo transData)
	{
		TransDataVo result = new TransDataVo<BillInfo>();
		if (!Tools.isEmpty(transData.getData())
				&& transData.getData() instanceof Map)
		{
			
			String dataObjec = JsonUtil.getJsonString(transData.getData());
			BillInfo billInfo = JsonUtil.fromJson(dataObjec, BillInfo.class);
			
			if (!Tools.isEmpty(billInfo.getCaroNo()))
			{
				try
				{
					ParkwellPayBillService payBill = srpingApp
							.getBean(ParkwellPayBillService.class);
					CarParkIn cpIn = payBill.selectCarNo(billInfo.getCaroNo());
					if (!Tools.isEmpty(cpIn))
					{
						/*LocalCarType carType = new LocalCarType();
						// 小型车
						carType.setCartypeId("MIN_CAR");
						String carTypeUuid = payBill.queryCarType(carType)
								.getCarTypeUuid();
						// 封装计费类
						LocalChargeBytime chargeBytime = new LocalChargeBytime();
						chargeBytime.setInTime(cpIn.getCreateTime());
						chargeBytime.setOutTime(new Date(System
								.currentTimeMillis()));
						chargeBytime.setCarType(carTypeUuid);*/
						/*ParkwellChargeBytimeService servevice = srpingApp
								.getBean(ParkwellChargeBytimeService.class);*/
						
						//计费引擎service
						//计费规则
						ParkwellChargeBytimeService localChargeByTimeService= srpingApp
								.getBean(ParkwellChargeBytimeService.class);
						
						//进场仓库表
						LocalCarInRepertoryService localCarInRepertoryService= srpingApp
										.getBean(LocalCarInRepertoryService.class);
						
						//商家电子优惠券service
						LocalCouponCashService localCouponCashService = srpingApp
								.getBean(LocalCouponCashService.class);
						
						
						/*double priceResult = servevice.queryBilling(
								chargeBytime, new ParkwellPayBillVo());*/
						//查询会员是否有效  service
						CarInAndOutService carInAndOutService= srpingApp
								.getBean(CarInAndOutService.class);
						
						LocalCarInRepertory carInRepertory = localCarInRepertoryService.findCarInRepertoryByCarInUuid(cpIn.getUuid());
						
						double priceResult=localChargeByTimeService.queryCarinBillMoney(carInRepertory.getChargeUuid(),cpIn.getCreateTime(),new Date(System.currentTimeMillis()));
						
						//判断车辆类型，是否会员，白名单，黑名单
						Map<String,Object> map = carInAndOutService
								.getIdentityTypeByCarNo(cpIn.getCarNo());
						String carNoType = (String) map.get("carNoType");
						// 系统会员,如果是会员/白名单   算出来的金额为0
						if(carNoType.equalsIgnoreCase("会员")||carNoType.equalsIgnoreCase("白名单")){
							priceResult=0d;
						}
						
						//查询已经支付的流水
						List<LocalPayBillCopy> listbillList = payBill.queryByCarInUuid(cpIn.getUuid());
						
						// 返回商家抵用劵集合  ,//查询抵用劵 ,状态为1 的所有集合
						List<LocalCouponCash> li = localCouponCashService.queryCouponCashByCarNo(cpIn.getCarNo());
						
						// 返回抵用劵集合
						List<CouponInfo> couponList = retCouponInfo(li,srpingApp);
						
						if(!Tools.isEmpty(listbillList)&&listbillList.size()!=0){
							for (LocalPayBillCopy bill : listbillList)
							{
								priceResult -= bill.getActualPrice();
							}
						}
						
						// 返回
						BillInfo billInfoResult = new BillInfo();
						billInfoResult.setCaroNo(billInfo.getCaroNo());
						billInfoResult.setPrice(priceResult<0?0d:priceResult*100);
						billInfoResult.setCouponList(couponList);
						result.setData(billInfoResult);
						result.setResult(CommonContans.STRUTS_SUCCESS);
						return result;
					}
				} catch (Exception e)
				{
					result.setResult(CommonContans.STRUTS_ERROR);
					result.setErrorCode("10002-1234-241-2341");
					result.setErrorMsg("未知异常");
				}
			}
		}
		return result;
	}
	
	public List<CouponInfo> retCouponInfo(List<LocalCouponCash> li,
			ApplicationContext srpingApp)
	{
		try
		{
			List<CouponInfo> couponList = new ArrayList<CouponInfo>();
			if (!Tools.isEmpty(li))
			{
				SimpleDateFormat ds=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				for (LocalCouponCash pcCash : li)
				{
					CouponInfo couponInfo = new CouponInfo();
					couponInfo.setBusinessName(!Tools.isEmpty(pcCash.getPartnerName())?pcCash.getPartnerName():null);
					couponInfo.setCouponType(CommonContains.PAY_TYPE_COUPON_CASH);
					couponInfo.setPrice(!Tools.isEmpty(pcCash.getPrice()) ? (int)(pcCash.getPrice()*100) : 0);
					couponInfo.setCreateTime(!Tools.isEmpty(pcCash.getCreateTime())?ds.format(pcCash.getCreateTime()):ds.format(new Date()));
					couponInfo.setUseTime(!Tools.isEmpty(pcCash.getUseTime())?pcCash.getUseTime():ds.format(new Date()));
					couponList.add(couponInfo);
				}
			}
			return couponList;
			
		} catch (Exception e)
		{
			// TODO: handle exception
			return null;
		}
	}
}
